package com.execdemo.demoorderservice.controller;

import com.execdemo.demoorderservice.enumerate.Result;
import com.execdemo.demoorderservice.feign.client.EventFeignClient;
import com.execdemo.demoorderservice.feign.client.MasterFeignClient;
import com.execdemo.demoorderservice.domain.Eventlist;
import com.execdemo.demoorderservice.domain.Orderlist;
import com.execdemo.demoorderservice.domain.Store;
import com.execdemo.demoorderservice.rabbitmq.MessageConsumer;
import com.execdemo.demoorderservice.service.OrderlistReviewService;
import com.execdemo.demoorderservice.service.OrderlistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderlistService orderService;

/*
    @Autowired
    private MasterFeignClient masterFeignClient;

    @Autowired
    private EventFeignClient eventFeignClient;
*/

    @Autowired
    private MessageConsumer messageConsumer;

    @Autowired
    private OrderlistReviewService orderlistReviewService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 模拟生产者端口
     * 接收HTTP POST请求，将接收到的JSON字符串作为消息发送到RabbitMQ队列中
     * 此方法主要用于处理订单相关的消息，将其发送到名为"mq"的队列
     *
     * @param mq 从请求体中获取的JSON字符串，代表订单信息
     */
    @PostMapping("/savemq")
    public void saveMQOrder(@RequestBody String mq) {
        // 记录接收到的JSON字符串，用于调试和审计
        log.info("接收到传输的JSON: " + mq);

        // 将接收到的JSON字符串作为消息，发送到RabbitMQ的"mq"队列中
        rabbitTemplate.convertAndSend("", "mq", mq);
    }

//    @GetMapping("/save")
//    public String saveMQOrder() {
//
//        List<Orderlist> orderlist = messageConsumer.getOrderMq();
//        List<Orderlist> orderlistPricePass = orderlistReviewService.reviewOrderlist(orderlist);
//
//        boolean save = orderService.saveOrUpdateBatch(orderlistPricePass) ;
//        if (save){
//            System.out.println(orderlist);
//            log.info("Received message: " + orderlist);
//            return "Orderlist Saved Successfully!!!";
//        } else {
//            return "Orderlist Saved Failed!!!";
//        }
//
//    }

/*

    @GetMapping("/getEvent/{code}")
    public Eventlist getEvent(@PathVariable("code") String code) {

        Eventlist eventlist = eventFeignClient.getEventlist(code);
        return eventlist;
    }
*/

/*

    @GetMapping("/getStore/{code}")
    public Store getStore(@PathVariable("code") String code) {

        Store store = masterFeignClient.getStore(code);
        return store;
    }
*/

/*    @PostMapping("/save")
    public String saveOrder(@RequestBody Orderlist order) {

        boolean save = orderService.save(order);
        return "Orderlist Saved Successfully!!!";
    }*/

    @PostMapping("/savetest")
    public String saveStoreTest() {
        System.out.println("saveTest");
        return "Test";
    }
}
